//
//  SKNumberChooserView.m
//  Sudoku
//
//  Created by Richard Hodgkins on 08/09/2012.
//  Copyright (c) 2012 Richard Hodgkins. All rights reserved.
//

#import "SKNumberChooserView.h"

#import "SKBoardConstants.h"

#define BUT_ROWS 3
#define BUT_COLS 3

#define TOTAL_BUTS (BUT_ROWS * BUT_COLS)

#define LINE_COLOR UIColorFromGrayscale(1, 0.1)
#define NORMAL_BUTTON_COLOR UIColorFromRGB(0x4616A6)
#define SELECTED_BUTTON_COLOR UIColorFromRGB(0xBF087F)
#define NORMAL_TEXT_COLOR UIColorFromRGB(0xFFFFFF)
#define SELECTED_TEXT_COLOR UIColorFromRGB(0xFFFFFF)

@interface SKNumberChooserView ()

@property (nonatomic, assign) id<SKNumberChooserDelegate> delegate;
@property (nonatomic, strong) NSArray *buttons;

@end

@implementation SKNumberChooserView

- (id)initWithFrame:(CGRect)frame andDelegate:(id<SKNumberChooserDelegate>) delegate
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.autoresizingMask = UIViewAutoresizingNone;
        self.backgroundColor = [UIColor clearColor];
        
        self.delegate = delegate;
        
        [self addContent];
                
        [self didReleaseNumber:[self.buttons objectAtIndex:0]];
    }
    return self;
}

- (void) addContent
{    
    NSMutableArray *buttons = [NSMutableArray arrayWithCapacity:(TOTAL_BUTS)];
    
    const CGSize buttonSize = CGSizeMake(self.bounds.size.width / BUT_ROWS, self.bounds.size.height / BUT_COLS);
    
    CGRect frame = self.bounds;
    frame.size = buttonSize;
    
    for (uint i=0; i<BUT_ROWS; i++) {
        for (uint j=0; j<BUT_COLS; j++) {
            const NSUInteger idx = j + (i * CELL_ROWS);
            const NSInteger num = idx + 1;
                                    
            UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
            but.frame = frame;
            but.tag = num;
            but.backgroundColor = [UIColor clearColor];
            but.titleLabel.textAlignment = NSTextAlignmentCenter;
            
            but.titleLabel.font = [but.titleLabel.font fontWithSize:IS_IPAD() ? 48 : 22];
            
            [but setTitleColor:NORMAL_TEXT_COLOR forState:UIControlStateNormal];
            [but setTitleColor:SELECTED_TEXT_COLOR forState:UIControlStateSelected];
            [but setTitle:[@(num) stringValue] forState:UIControlStateNormal];
            [but addTarget:self action:@selector(didReleaseNumber:) forControlEvents:UIControlEventTouchUpInside];
            [but addTarget:self action:@selector(didPressedNumber:) forControlEvents:UIControlEventTouchDragInside];
            
            [buttons addObject:but];
            
            [self addSubview:but];
            
            frame.origin.x += buttonSize.width;
        }
        frame.origin.x = self.bounds.origin.x;
        frame.origin.y += buttonSize.height;
    }
    
    self.buttons = buttons;
}

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    
    // Drawing code
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    const CGRect bounds = self.bounds;
    const CGFloat originX = bounds.origin.x;
    const CGFloat originY = bounds.origin.y;
    const CGFloat width = bounds.size.width;
    const CGFloat height = bounds.size.height;
    const CGFloat thirdX = width / BUT_ROWS;
    const CGFloat thirdY = height / BUT_COLS;
    
    // Draw the button backgrounds
    CGContextSaveGState(ctx);
    for (UIButton *button in self.buttons) {
        if ([button isSelected]) {
            CGContextSetFillColorWithColor(ctx, SELECTED_BUTTON_COLOR.CGColor);
        } else {
            CGContextSetFillColorWithColor(ctx, NORMAL_BUTTON_COLOR.CGColor);
        }
        CGContextFillRect(ctx, button.frame);
    }
    CGContextRestoreGState(ctx);
    
    
    CGContextSaveGState(ctx);
    CGContextSetLineWidth(ctx, 2);
    CGContextSetStrokeColorWithColor(ctx, LINE_COLOR.CGColor);
    
    // Draw vertical seperators
    CGFloat x = originX, y = originY;
    int endY = y + height;
    for (uint i=1; i<BUT_ROWS; i++) {
        x += thirdX;
        CGContextMoveToPoint(ctx, (int) x, (int) y);
        CGContextAddLineToPoint(ctx, (int) x, endY);
    }
    
    // Draw horizontal seperators
    x = originX, y = originY;
    int endX = x + width;
    for (uint i=1; i<BUT_COLS; i++) {
        y += thirdY;
        CGContextMoveToPoint(ctx, (int) x, (int) y);
        CGContextAddLineToPoint(ctx, endX, (int) y);
    }

    CGContextStrokePath(ctx);
    CGContextRestoreGState(ctx);
}

#pragma mark - Button pressd delegates

- (void) didReleaseNumber:(UIButton *) pressed
{
    for (UIButton *b in self.buttons) {
        b.selected = b == pressed;
    }
    
    [self.delegate numberWasSelected:pressed.tag];
    
    [self setNeedsDisplay];
}

- (void) didPressedNumber:(UIButton *) pressed
{
    [self setNeedsDisplay];
}

@end
